import 'dart:io';

void main() {
  List<Map<String, double>> incomeList = [];
  List<Map<String, double>> expenseList = [];
  
  while (true) {
    // Display the menu
    print('Budget Management Menu:');
    print('1. Add Income');
    print('2. Add Expense');
    print('3. View Summary');
    print('4. Exit');
    stdout.write('Enter your choice (1-4): ');
    String? choice = stdin.readLineSync();

    if (choice == '1') {
      // Add income
      stdout.write('Enter income description: ');
      String description = stdin.readLineSync()!;
      stdout.write('Enter income amount: ');
      double? amount = double.tryParse(stdin.readLineSync()!);
      
      if (amount != null && amount >= 0) {
        incomeList.add({description: amount});
        print('Income added.');
      } else {
        print('Invalid amount! Please enter a positive value.');
      }

    } else if (choice == '2') {
      // Add expense
      stdout.write('Enter expense description: ');
      String description = stdin.readLineSync()!;
      stdout.write('Enter expense amount: ');
      double? amount = double.tryParse(stdin.readLineSync()!);
      
      if (amount != null && amount >= 0) {
        expenseList.add({description: amount});
        print('Expense added.');
      } else {
        print('Invalid amount! Please enter a positive value.');
      }

    } else if (choice == '3') {
      // View summary
      double totalIncome = incomeList.fold(0, (sum, item) => sum + item.values.first);
      double totalExpenses = expenseList.fold(0, (sum, item) => sum + item.values.first);
      double netProfit = totalIncome - totalExpenses;
      
      print('--- Budget Summary ---');
      print('Total Income: \$$totalIncome');
      print('Total Expenses: \$$totalExpenses');
      if (netProfit >= 0) {
        print('Net Profit: \$$netProfit');
        double tax = netProfit * 0.15;
        print('Tax (15%): \$$tax');
      } else {
        print('Net Loss: \$$netProfit');
      }
    } else if (choice == '4') {
      // Exit the program
      print('Exiting...');
      break;
    } else {
      print('Invalid choice! Please select a valid option.');
    }
  }
}
